<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页预览进度条</title>
    <link rel="stylesheet" href="../css/common.css">
    <style>
        #progress{
            width: 0vw;
            height: 2px;
            position: fixed;
            top: 0;
            background-color: darkgreen;
        }
        #box{
            height: 5000px;
            background-color: lightpink;
        }
    </style>
</head>
<body>
    <div id="progress"></div>
    <div id="box"></div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <script>
        $(window).scroll(function(e){
            //height()方法，用于获取高度
            //innerHeight()方法，用于获取可见高度
            //获取总高
            let all_height = $(document).height() - $(window).innerHeight()
            //获取滚动的高度
            let top = $(window).scrollTop()
            //算出占比
            let percent = top / all_height * 100 + '%'
            $('#progress').css('width',percent)
        })
    </script>
</body>
</html>